<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * 用户
 */
class User extends Model
{
    protected $table = 'users';

    protected $fillable = [
        'id',
        'account', //账号
        'nickname', //昵称
        'email', //手机
        'phone', //手机
        'password', //密码
        'pay_pass', //交易密码
        'headimg', //头像
        'status', //状态 1为正常 0为废弃
        'real_status', //实名认证状态 1 未认证 2已认证 3 审核中 4 审核失败
        'type', //身份 1会员 2商家
        'attestation', //商家认证状态 1未认证 2已认证
        'attesta_time',  //认证通过时间
        'yx_status', //1 云信未注册  2云信已注册
        'islogin',  //登录状态 0 未登录 1已登录
        'token', //验证token
    ];

    //类型定义
    const REAL_STATUS_ONE      = 1;
    const REAL_STATUS_TWO      = 2;
    const REAL_STATUS_THREE    = 3;
    const REAL_STATUS_FOUR     = 4;
    public static $REAL_STATUS = [
        self::REAL_STATUS_ONE   => '未认证',
        self::REAL_STATUS_TWO   => '已认证',
        self::REAL_STATUS_THREE => '审核中',
        self::REAL_STATUS_FOUR  => '审核失败',
    ];

    const STATUS_ONE      = 1;
    const STATUS_TWO      = 2;
    public static $STATUS = [
        self::STATUS_ONE   => '正常',
        self::STATUS_TWO   => '废弃',
    ];

    //关联委托单
    public function entrusts()
    {
        return $this->hasMany('App\\Models\\Entrust', 'uid', 'id');
    }
    //关联交易单
    public function orders()
    {
        return $this->hasMany('App\\Models\\Order', 'uid', 'id');
    }
    //用户银行卡
    public function user_bank()
    {
        return $this->hasOne('App\\Models\\UserBank', 'uid', 'id');
    }
    //用户支付宝
    public function user_alipay()
    {
        return $this->hasOne('App\\Models\\UserAlipay', 'uid', 'id');
    }
    //用户微信
    public function user_wechat()
    {
        return $this->hasOne('App\\Models\\UserWechat', 'uid', 'id');
    }
    //商家挂单表
    public function rech_orders()
    {
        return $this->hasMany('App\\Models\\RechOrder', 'uid', 'id');
    }
    //法币交易（商家关联）
    public function recharges()
    {
        return $this->hasMany('App\\Models\\Recharge', 'bid', 'id');
    }
    //商家近三十日交易
    public function thirty_summary()
    {
        //30天前日期
        $thirtyData = date('Y-m-d H:i:s', strtotime('-30 days'));
        $where['status'] = 3;
        return $this->recharges()->where($where)->where(function ($query) use ($thirtyData) {
            $query->where('created_at', '>', $thirtyData);
        });
    }
    //商家近三十日交易
    public function thirty_all()
    {
        //30天前日期

        $where['status'] = 3;
        return $this->recharges()->where($where);

    }
        //关联解绑
    public function unbind()
    {
        return $this->hasOne('App\\Models\\Unbind', 'uid', 'id');
    }
    //实名
    public function autonym()
    {
        return $this->hasOne('App\\Models\\Autonym', 'uid', 'id');
    }
    //关联所有资产
    public function property()
    {
        return $this->hasMany('App\\Models\\Property', 'uid', 'id');
    }
    //关联指定币种资产
    public function currency_property($cid)
    {
        return $this->property()->where(function ($query) use ($cid) {
            $query->where('cid', $cid);
        });
    }
    //登录历史
    public function login_log()
    {
        return $this->hasMany('App\\Models\\LoginLog', 'uid', 'id')->orderBy('created_at', 'desc');
    }

    //关联商家申请
    public function enters()
    {
        return $this->hasOne('App\\Models\\Enter', 'uid', 'id')->orderBy('created_at', 'desc');
    }

    /**
     * 绑定支付信息
     * @date 2018/03/26
     * @author yanggang
     * @param $params
     * @return bool
     */
    public function savePayInfo($params)
    {
        $type = $params['type']; //0 绑银行卡 1 绑支付宝账户 2 绑微信账户
        if ($type == 0){
            $uid = $params['uid'];
            $bank_no = $params['bank_name'];
            $bank_info = $params['bank_name'];
            $data = [
                'uid'=>$uid,
                'bank_no'=>$bank_no,
                'bank_info'=>$bank_info,
            ];
            if ($data['id'] || ($res = UserBank::where(['uid'=>$uid,'bank_no'=>$bank_no,'bank_info'=>$bank_info])->first())){
                $id = $data['id'] ?? $res['id'];
                UserBank::where(['id'=>$id])->update($data);
            }else{
                UserBank::create($data);
            }
        }elseif($type == 1){
            $uid = $params['uid'];
            $account = $params['account'];
            $code = $params['code'];
            $data = [
                'uid'=>$uid,
                'alipay_account'=>$account,
                'alipay_code'=>$code,
            ];
            if ($data['id'] || ($res = UserAlipay::where(['uid'=>$uid])->first())){
                $id = $data['id'] ?? $res['id'];
                UserAlipay::where(['id'=>$id])->update($data);
            }else{
                UserAlipay::create($data);
            }
        }elseif($type == 2){
            $uid = $params['uid'];
            $account = $params['account'];
            $code = $params['code'];
            $data = [
                'uid'=>$uid,
                'wechat_account'=>$account,
                'wechat_code'=>$code,
            ];
            if ($data['id'] || ($res = UserWechat::where(['uid'=>$uid])->first())){
                $id = $data['id'] ?? $res['id'];
                UserWechat::where(['id'=>$id])->update($data);
            }else{
                UserWechat::create($data);
            }
        }

        return true;


    }
}
